package com.imooc.town.controller;

import com.imooc.town.config.ProjectUrlConfig;
import com.imooc.town.constant.CookieConstant;
import com.imooc.town.constant.RedisConstant;
import com.imooc.town.dataobject.SellerInfo;
import com.imooc.town.enums.ResultEnum;
import com.imooc.town.service.SellerService;
import com.imooc.town.utils.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * create by song on 2018/3/25
 * To change this template use File | Settings | File Templates.
 */

@Controller
@RequestMapping("/seller")
public class SellerUserController {

    @Autowired
    private SellerService sellerService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ProjectUrlConfig projectUrlConfig;


    @GetMapping("/login")
    public ModelAndView login(@RequestParam("openid") String openid, HttpServletResponse response, Map<String ,Object> map){

//        openid取数据库里面匹配
        SellerInfo sellerInfo = sellerService.findSellerInfoByOpenid ( openid );
        if (sellerInfo == null){
            map.put("msg", ResultEnum.LOGIN_FAIL.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("common/error");
        }

//        2、设置token到redis
        String token = UUID.randomUUID ().toString ();
        Integer expire = RedisConstant.EXPIRE;
        redisTemplate.opsForValue ().set ( String.format ( RedisConstant.TOKEN_PREFIX,token ),openid,expire, TimeUnit.SECONDS );
//        3、设置token到cookie
        CookieUtil.set ( response, CookieConstant.TOKEN,token,expire );
        return new ModelAndView ( "redirect:"+projectUrlConfig.getSell() + "/sell/seller/order/list" );
    }



    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request,
                               HttpServletResponse response,
                               Map<String, Object> map) {
        //1. 从cookie里查询
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if (cookie != null) {
            //2. 清除redis
            redisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));

            //3. 清除cookie
            CookieUtil.set(response, CookieConstant.TOKEN, null, 0);
        }

        map.put("msg", ResultEnum.LOGOUT_SUCCESS.getMessage());
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("common/success", map);
    }
}
